'use strict';

App.controller('publishController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
	$scope.publishQuery = {};
	$scope.customerId =	$rootScope.user.sourceId;// 根据登录用户获得客户id
	if($scope.customerId == '' || $scope.customerId == null){
		$state.go('login', null);
		Page.notice("获取登录信息失败!");
		return;
	}
    var date = new Date();
    var now = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
	
    $scope.floors = [{
        name : '单层',
        value : '1'
    },{
        name : '双层',
        value : '2'
    }]

	
	$scope.publishTable = {
			query : function() {
				var param = {};
				param.customerId = $scope.customerId;
				param.billNo = $scope.publishQuery.billNo;
				param.pageIndex = 0;
				param.pageSize = 10;
				Page.ajaxPost('www/publishBill/findAdPublishBill.do', param, function (resp) {
					 $scope.dataArray = resp.data.dataArray;
					 
					// 调用分页
				    	$scope.totalPages = resp.data.totalPages;
						laypage({
							cont : 'page_bar',
							pages : $scope.totalPages,// 得到总页数
							skin : '#3E97E4',
							skip : true, // 是否开启跳页
							prev : '<', // 若不显示，设置false即可
							next : '>', // 若不显示，设置false即可
							groups : 5, //连续分页数
							curr: 1, //初始化当前页
							jump : function(obj,first) {// 触发分页后的回调
								/*obj是一个object类型。包括了分页的所有配置信息。
								 first一个Boolean类，检测页面是否初始加载。非常有用，可避免无限刷新。*/
								if(!first){
									param.pageIndex = obj.curr - 1;
									Page.ajaxPost('www/publishBill/findAdPublishBill.do', param, function(resp) {
										$scope.dataArray = resp.data.dataArray;
										$scope.totalPages = resp.data.totalPages;
									});
								}
							}
						})
				 });
			},
			deleteBill : function(data) {
		            Page.confirm("确定要删除吗？", function () {
		                Page.ajaxPost('publishBill/delete.do', data, function () {
		                    $scope.publishTable.query();
		                });
		            });
		        },
	        add : function() {// 编辑
	        	var data = {};
	        	data.addFlag = true;
	        	$state.go('addPublish', {model: data});
	        	return;
		        },
			update : function(data) {// 编辑
				  $state.go('addPublish', {model: data});
				  return;
		        },
	    	view : function(data) {// 查看
				  $state.go('viewPublish', {model: data});
				  return;
		        },
	        viewMakeBillInfo : function(data) {// 查看制作单信息(包含明细)
				  $state.go('publishMakeBillDetail', {model: data});
				  return;
		        },
	        start : function(data) {// 提交审核
	            Page.confirm("确定要提交审核吗？", function () {
	                Page.ajaxPostQuiet('publishBill/startPublishBill.do', {billId : data.billId}, function () {
	                    Page.notice("提交审核成功!");
	                    $scope.publishTable.query();
	                });
	            });

	        },
	}
	$scope.publishTable.query();
	
	
	
	
}]);